﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OnlineAuction.Business;
using OnlineAuction.Entity;

namespace OnlineAuction.Admin
{
    public partial class MergeCategory : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Cookies["UserInfo"] == null
                || Request.Cookies["UserInfo"]["Role"].ToString().CompareTo("Admin") != 0)
            {
                Response.Redirect("../Home.aspx");
            }
            else if (!Page.IsPostBack)
            {
                //Get all category
                CategoryBO categoryBO = new CategoryBO();
                ddlCategoryName1.DataTextField = "CategoryName";
                ddlCategoryName1.DataValueField = "CategoryID";
                ddlCategoryName1.DataSource = categoryBO.GetCategory();
                ddlCategoryName1.DataBind();
                ddlCategoryName1.SelectedIndex = 1;
                ddlCategoryName2.DataTextField = "CategoryName";
                ddlCategoryName2.DataValueField = "CategoryID";
                ddlCategoryName2.DataSource = categoryBO.GetCategory();
                ddlCategoryName2.DataBind();
                ddlCategoryName2.SelectedIndex = 2;
            }
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
        }

        //Add new item
        protected void btnAddItem_Click(object sender, EventArgs e)
        {
            CategoryBO cbo = new CategoryBO();
            //create new category entity
            CategoryE ce1 = new CategoryE(Convert.ToInt32(ddlCategoryName1.SelectedValue));
            CategoryE ce2 = new CategoryE(Convert.ToInt32(ddlCategoryName2.SelectedValue));

            //Check result add category
            bool flag = cbo.MergeCategory(ce1, ce2, txtCategoryName.Text);
            if (flag)
            {
                Response.Write("<script>alert('Success');</script>");
                Response.Redirect("MergeCategory.aspx");
            }
            else
            {
                Response.Write("<script>alert('Error');</script>");
            }
        }
    }
}